package com.cljh.mtmall.product.service.impl;

import com.cljh.mtmall.product.dao.AttrDao;
import com.cljh.mtmall.product.entity.AttrEntity;
import com.cljh.mtmall.product.service.AttrService;
import com.cljh.mtmall.product.vo.AttrGroupWithAttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cljh.common.utils.PageUtils;
import com.cljh.common.utils.Query;

import com.cljh.mtmall.product.dao.AttrGroupDao;
import com.cljh.mtmall.product.entity.AttrGroupEntity;
import com.cljh.mtmall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catalogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)){
            wrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }
        if (catalogId==0){
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        }else{
            wrapper.eq("catelog_id",catalogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        }

    }

    /**
     * 根据分类id查出所有分组以及分组的里面的属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {

        //1.查询分组信息
        List<AttrGroupEntity> catelog_id = this.list(
                new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));


        System.out.println("catelog_id:"+catelog_id);

        List<AttrGroupWithAttrVo> collect = null;
        if (catelog_id != null && catelog_id.size() > 0) {
            //2.查询所有属性
            collect = catelog_id.stream().map(group -> {
                AttrGroupWithAttrVo attrGroupWithAttrVo = new AttrGroupWithAttrVo();
                BeanUtils.copyProperties(group, attrGroupWithAttrVo);
                Long attrGroupId = attrGroupWithAttrVo.getAttrGroupId();
                if (attrGroupId!=null){
                    List<AttrEntity> attr = attrService.getRelationAttr(attrGroupId);
                    if(attr!=null&&attr.size()>0){
                        attrGroupWithAttrVo.setAttrs(attr);
                    }
                }
                return attrGroupWithAttrVo;
            }).collect(Collectors.toList());
        }

        System.out.println("collect"+collect);
        return collect;
    }

}